package com.rfid.emergencywarehouse.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Html;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.rfid.base.base.BaseActivity;
import com.rfid.base.base.BasePresenter;
import com.rfid.base.util.BaseUtil;
import com.rfid.base.util.LogUtils;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.reader.RfidManager;
import com.rfid.emergencywarehouse.util.DialogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HomeAcitvity extends BaseActivity<BasePresenter> {
    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    String mName;

    @BindView(R.id.user_welcome)
    TextView mWelcome;

    @BindView(R.id.power)
    TextView mPower;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        LogUtils.v("HomeAcitvity");
        ButterKnife.bind(this);
        mName = getIntent().getStringExtra("name");
        mWelcome.setText(mName + "，您好！");

        showPower();

        mPower.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText = new EditText(HomeAcitvity.this);
                editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
                new AlertDialog.Builder(HomeAcitvity.this)
                        .setTitle("设置功率")
                        .setView(editText)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String epower = editText.getText().toString();
                                if (TextUtils.isEmpty(epower)) {
                                    return;
                                }
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        String msg = "请输入数字功率";
                                        try {
                                            int power = Integer.valueOf(epower);
                                            boolean s = RfidManager.getInstance().setPower(power);
                                            msg = s?"设置成功":"设置失败";
                                        } catch (Exception e) {
                                            LogUtils.e(e);
                                        }
                                        String finalMsg = msg;
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(HomeAcitvity.this, finalMsg,Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                        showPower();
                                    }
                                }).start();
                            }
                        }).setNegativeButton("取消",null)
                        .show();
            }
        });
    }

    private void showPower() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int power = RfidManager.getInstance().getPower();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (power == 0) {
                            mPower.setText("");
                        } else {
                            mPower.setText(Html.fromHtml(String.format("<u>功率：%s</u>",power+""), Html.FROM_HTML_MODE_LEGACY));
                        }
                    }
                });
            }
        }).start();
    }

    @OnClick({R.id.xjsjd})
    public void xjsjd(View v){
        BaseUtil.startActivity(this,CreateListingActivity.class,null);
    }
    @OnClick({R.id.ydlq})
    public void ydlq(View v){
            BaseUtil.startActivity(this,GetAssetWithOrderActivity.class,null);
    }
    @OnClick({R.id.wdlq})
    public void wdlq(View v){
        BaseUtil.startActivity(this,GetAssetWithoutOrderActivity.class,null);
    }
    @OnClick({R.id.xzwz})
    public void xzwz(View v){
        BaseUtil.startActivity(this,AddAssetActivity.class,null);
    }
    @OnClick({R.id.wzgh})
    public void wzgh(View v){
        BaseUtil.startActivity(this,AssetReturnActivity.class,null);
    }
    @OnClick({R.id.wdsj})
    public void wdsj(View v){
        BaseUtil.startActivity(this, ShelfingWithoutOrderActivity.class,null);
    }
    @OnClick({R.id.ydsj})
    public void ydsj(View v){
        BaseUtil.startActivity(this, ShelfingWithOrderActivity.class,null);
    }
    @OnClick({R.id.pd})
    public void pandian(View v){
        BaseUtil.startActivity(this, PanDianActivity.class,null);
    }

    long lastClickTime;
    @Override
    public void onBackPressed() {
        long now = System.currentTimeMillis();
        if (now - lastClickTime < 4000) {
            finish();
        } else {
            lastClickTime = now;
            DialogUtil.showTipDialog(this,findViewById(R.id.root),"再按一次退出");
        }
    }
}
